package com.shop.cloud.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.shop.cloud.common.mybatis.BaseController;
import com.shop.cloud.domain.system.SysRole;
import com.shop.cloud.domain.system.SysUser;
import com.shop.cloud.filter.CurrentUserId;
import com.shop.cloud.filter.MagicPermission;
import com.shop.cloud.service.system.IRoleResourceService;
import com.shop.cloud.service.system.ISysRoleService;
import com.shop.cloud.service.system.ISysUserService;
import com.shop.cloud.util.DateTimeUtil;
import com.shop.cloud.util.Page;
import com.shop.cloud.util.Response;
import com.shop.cloud.util.ResponseUtil;

@RestController
@RequestMapping(value = "role")
public class SysRoleController extends BaseController {

	private static final Logger logger=Logger.getLogger(SysRoleController.class);
	
	@Autowired
	private ISysRoleService sysRoleService;
	@Autowired
	private IRoleResourceService roleResourceService;
	
	@MagicPermission
	@RequestMapping(value = "updateUserRole", method = RequestMethod.POST)
	@ResponseBody
	public Response updateUserRole(HttpServletRequest request,boolean isAdd) {
		if(isAdd){
			sysRoleService.insertUserRole(getParamMap(request));
		}else{
			sysRoleService.removeUserRole(getParamMap(request));
		}
		return ResponseUtil.ok();
	}
	
	
	@MagicPermission
	@RequestMapping(value = "findUserRole", method = RequestMethod.POST)
	@ResponseBody
	public Response findUserRole(Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		List<SysRole> datas=sysRoleService.find(params);
		List<String> roleIds=sysRoleService.findUserRole(userId);
		List<Map<String, Object>> dataList=new ArrayList<Map<String,Object>>();
		for (int i = 0; i < datas.size(); i++) {
			SysRole role=datas.get(i);
			Map<String, Object> dataMap=new HashMap<String, Object>();
			dataMap.put("id", role.getId());
			boolean hasValue=false;
			for (String roleId : roleIds) {
				if(roleId.equals(role.getId())){
					hasValue=true;
				}
			}
			if (hasValue) {
				dataMap.put("check", true);
			} else {
				dataMap.put("check", false);
			}
			dataMap.put("name", role.getName());
			dataMap.put("createUser", role.getCreateUser());
			dataList.add(dataMap);
		}
		return ResponseUtil.ok(dataList);
	}
	
	@MagicPermission
	@RequestMapping(value = "find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(@CurrentUserId Long userId,HttpServletRequest request){
		Map<String, Object> params=getPageMap(request);
		Page<SysRole> datas=sysRoleService.page(params);
		return ResponseUtil.ok(datas);
	}
	
	@MagicPermission
	@RequestMapping(value = "list")
	@ResponseBody
	public Response list(HttpServletRequest request){
		return ResponseUtil.ok(sysRoleService.list());
	}
	
	@MagicPermission
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(HttpServletRequest request,SysRole sysRole){
		sysRoleService.update(sysRole);
		return ResponseUtil.ok();
	}
	
	@MagicPermission
	@RequestMapping(value = "insertTwo")
	@ResponseBody
	public Response insertTwo(String removeResourceIds,String insertResourceIds,String roleId){
		sysRoleService.insertTwo(insertResourceIds,roleId);
		return ResponseUtil.ok();
	}
	
	@MagicPermission
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,HttpServletRequest request,SysRole sysRole,String moduleIds){
		sysRole.setCreateDate(DateTimeUtil.getSystemDate());
		sysRole.setCreateUser(userId);
		sysRoleService.insert(sysRole,moduleIds);
		return ResponseUtil.ok();
	}

 	@MagicPermission
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,SysRole sysRole){
		HashMap<String, Object> param= new HashMap<String, Object>();
		param.put("id", sysRole.getId());
		SysRole r = sysRoleService.find(param).get(0);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("roleId", r.getId());
		Long roleId = r.getId();
		System.out.println(sysRoleService.findRole(roleId).size());
		if(sysRoleService.findRole(r.getId()).size()>0){
			return ResponseUtil.error("有账户正在使用此角色，无法删除！");
		}else{
			sysRoleService.removeUserRole(map);
			roleResourceService.remove(map);
			sysRoleService.remove(param);
		}
		return ResponseUtil.ok();
	}

}